package nGui.controls;

import nGui.controls.ReturnType;

class Control
{
	public var __c(default,null) : Void;
	public var bgColor(null,setBgColor) : Int;
	public var fgColor(null,setFgColor) : Int;
	private var __w : Int;
	private var __h : Int;
	public var active(null,setActive) : Bool;
	public var expand(null,setExpand) : Bool;
	public var title(null,setTitle) : String;
	public var width(getWidth,setWidth) : Dynamic;
	public var height(getHeight,setHeight) : Dynamic;
	public var value(getValue,setValue) : Dynamic;
	public var tip(getTip,setTip) : String;
	public var visible(getVisible,setVisible) : Bool;
	public var returnType(default,default) : ReturnType;

	public function new()
	{

	}

	public function init()
	{
		var s : String = getAttribute( "SIZE" );
		neko.Lib.print( "\n\nSize is " + s + "\n" );
		if (s != null)
		{
			var d = s.split('x');
			__w = Std.parseInt( d[0] );
			__h = Std.parseInt( d[1] );
		}
	}

	private function setActive( val : Bool )
	{
		setAttribute( "ACTIVE", if ( val == true ) "YES" else "NO" );
		return null;
	}

	private function setExpand( val : Bool )
	{
		setAttribute( "EXPAND", if ( val == true ) "YES" else "NO" );
		return null;
	}
	
	private function setCallback( type : String, fun : Dynamic, ?ret : ReturnType )
	{
		if ( ret == null )
			ret = DEFAULT;
		var r : Int = RTToInt( ret );
		ngui_set_callback( __c, neko.Lib.haxeToNeko( type ), fun, r );
	}

	public function RTToInt( ret : ReturnType ) : Int
	{
		return switch( ret )
		{
			case DEFAULT:
				-2;
			case CLOSE:
				-3;
			case CONTINUE:
				-4;
			default:
				-1;
		}
	}

	private function setAttribute( type : String, value : String, ?control : Control )
	{
		if ( value != null && type != null )
		{
			//neko.Lib.print( "\n\n" + type + " = " + value + "\n" );
			if ( control == null )
				ngui_set_attribute( __c, neko.Lib.haxeToNeko( type ), neko.Lib.haxeToNeko( value ) );
			else
				ngui_set_attribute( control.__c, neko.Lib.haxeToNeko( type ), neko.Lib.haxeToNeko( value ) );
		}
	}

	private function getAttribute( type : String ) : String
	{
		neko.Lib.print("getting : " + type );
		return neko.Lib.nekoToHaxe( ngui_get_attribute( __c, untyped type.__s ) );
	}
	
	public function tabTitle( title : String )
	{
		setAttribute( "TABTITLE", title );
		return null;
	}

	private function setBgColor( val : Int )
	{
		var r : Int = (val & 0xFF0000) >> 16;
		var g : Int = (val & 0x00FF00) >> 8;
		var b : Int = val & 0x0000FF;
		this.setAttribute( "BGCOLOR", Std.string(r)+" "+Std.string(g)+" "+Std.string(b) );
		return null;
	}

	private function setFgColor( val : Int )
	{
		var r : Int = (val & 0xFF0000) >> 16;
		var g : Int = (val & 0x00FF00) >> 8;
		var b : Int = val & 0x0000FF;
		this.setAttribute( "FGCOLOR", Std.string(r)+" "+Std.string(g)+" "+Std.string(b) );
		return null;
	}

	private function setTitle( val : String )
	{
		this.setAttribute( "TITLE", val );
		return null;
	}
	
	function setTip( val : String )
	{
		this.setAttribute( "TIP", untyped val );
		return null;
	}
	
	function getTip() : String
	{
		return this.getAttribute( "TIP" );
	}
	
	function setVisible( val : Bool )
	{
		this.setAttribute( "VISIBLE", ( val ) ? "YES" : "NO" );
		return null;
	}
	
	function getVisible() : Bool
	{
		return ( this.getAttribute( "VISIBLE" ) == "YES" );
	}


	function setValue( val : Dynamic )
	{
		this.setAttribute( "VALUE", Std.string( val ) );
		return null;
	}

	function getValue() : Dynamic
	{
		var val : Dynamic = this.getAttribute( "VALUE" );
		return Std.string( val );
	}

	public function setSize( w : String, h : String ) : Void
	{
		this.setAttribute( "SIZE", w+"x"+h );
	}

	public function setHeight( h : Dynamic )
	{
		setSize( Std.string( __w ), Std.string( h ) );
		return h;
	}

	public function getHeight() : Dynamic
	{
		return __h;
	}

	public function setWidth( w : Dynamic )
	{
		setSize( Std.string( w ), Std.string( __h ) );
		return w;
	}

	public function getWidth() : Dynamic
	{
		return __w;
	}

	public function setHandle( handle : String )
	{
		ngui_set_handle( this.__c, untyped handle.__s );
	}

	public function map()
	{
		ngui_map( this.__c );
	}
	
	static var ngui_set_callback = neko.Lib.load("nGui","ngui_set_callback",4);
	static var ngui_set_attribute = neko.Lib.load("nGui","ngui_set_attribute",3);
	static var ngui_get_attribute = neko.Lib.load("nGui","ngui_get_attribute",2);
	static var ngui_set_handle = neko.Lib.load("nGui","ngui_set_handle",2);
	static var ngui_map = neko.Lib.load("nGui","ngui_element_map",1);
}